<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Message Extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		if($this->session->userdata('logged_in') == FALSE)
		{
			redirect();
		}else{
			$this->load->model('M_message','me');
			$this->view_data['system_message'] =$this->_msg();
		}
		$this->view_data['tiny_mce'] = '<script type="text/javascript" src="'.base_url('assets/tinymce/tinymce.min.js').'"></script>';

	}
	
	public function process()
	{
		if(IS_AJAX)
		{
			$post = $this->input->post();
			pp($post);
			exit();
		}else{

			$post = $this->input->post();
			if($this->input->post('send_message'))
			{
				
				$result = $this->me->process_message($post);
				
				if($result)
				{
					$this->_msg('s','Message Was Sent','message');
				}else{
					$this->_msg('e','Unable to send message','message');
				}
			}elseif($this->input->post('reply_to_thread'))
			{
				$result = $this->me->reply_to_thread($post);

				if($result)
				{
					$this->_msg('s','Message Was Sent','message/thread/'.$post['hash']);
				}else{
					$this->_msg('e','Unable to send message','message');
				}
			}
		}
	}

	public function index()
	{
		$user_id = $this->session->userdata('loc_emp_id');
		$this->view_data['employees'] = $this->me->get_all_employees();
		if($this->session_user != 'teacher')
		{
			if($this->current_school_year==3){
				$this->view_data['students'] = $this->me->get_legal_students(FALSE,2);
				$this->view_data['parents'] = $this->me->get_legal_parents(FALSE,2);
				$this->view_data['sections'] = $this->me->get_all_sections(FALSE,2);
			}else{
				$this->view_data['students'] = $this->me->get_legal_students();
				$this->view_data['parents'] = $this->me->get_legal_parents();
				$this->view_data['sections'] = $this->me->get_all_sections();
			}
		}else{
			if($this->current_school_year==3){
				$this->view_data['students'] = $this->me->get_legal_students($user_id,2);
				$this->view_data['parents'] = $this->me->get_legal_parents($user_id,2);
				$this->view_data['sections'] = $this->me->get_all_sections($user_id,2);
			}else{
				$this->view_data['students'] = $this->me->get_legal_students($user_id);
				$this->view_data['parents'] = $this->me->get_legal_parents($user_id);
				$this->view_data['sections'] = $this->me->get_all_sections($user_id);
			}
		}
		$this->view_data['gradelevels'] = $this->me->get_all_levels();
		$this->view_data['myinbox'] = $this->me->inbox();
		$this->view_data['delbox'] = $this->me->delbox();
		$this->view_data['count_viewer'] = function($thread_id){return $this->me->count_viewer($thread_id);};
	}

	public function thread($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$thread_id = $x[0];
			$this->me->set_read($thread_id);
			$this->view_data['thread'] = $this->me->show_thread($thread_id);
			$this->view_data['thread_id'] = $thread_id;
			$this->view_data['hash'] = $hash;
		}else{
			$this->_msg('n','Invalid URL Supplied','message');
		}
	}
	
	public function delete($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$thread_id = $x[0];
			$result = $this->me->remove_me($thread_id);
			if($result)
			{
				$this->_msg('s','Thread deleted.','message');
			}else{
				$this->_msg('e','Unable to delete thread.','message');
			}
		}else{
			$this->_msg('n','Invalid URL supplied.','message');
		}
	}
	
	public function del_permanently($hash)
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$thread_id = $x[0];
			$result = $this->me->remove_me_permanently($thread_id);
			if($result)
			{
				$this->_msg('s','Permanently deleted.','message');
			}else{
				$this->_msg('e','Unable to delete thread.','message');
			}
		}else{
			$this->_msg('n','Invalid URL supplied.','message');
		}
	}
	
	public function remove_me($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$thread_id = $x[0];
			$result = $this->me->remove_me_also($thread_id);
			if($result)
			{
				$this->_msg('s','Successfully removed from the thread.','message');
			}else{
				$this->_msg('e','Unable to remove you from the thread.','message');
			}
		}else{
			$this->_msg('n','Invalid URL supplied.','message');
		}
	}
	
	public function all_archived()
	{
		$this->view_data['allinbox'] = $this->me->allinbox();
	}
	
	public function archivethread($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$thread_id = $x[0];
			$this->view_data['thread'] = $this->me->show_thread($thread_id);
			$this->view_data['thread_id'] = $thread_id;
			$this->view_data['hash'] = $hash;
		}else{
			$this->_msg('n','Invalid URL Supplied','message');
		}
	}
}